/**
 * $Id: Utils.java 14 2011-02-01 08:06:29Z andriusms $
 */
package com.googlecode.kanjicards.format;

import com.itextpdf.text.pdf.*;
import org.apache.log4j.*;

import java.io.*;

public final class Utils {

    private static final Logger log = Logger.getLogger(Utils.class);

    private Utils() {
    }

    public static FileOutputStream fileOutputStream(String filename) throws FileNotFoundException {
        File file = new File(filename);
        if(!file.getParentFile().exists()) file.getParentFile().mkdirs();
        return new FileOutputStream(file);
    }

    public static BaseFont getJapaneseFont(String fontFile) throws RenderingException {
        BaseFont font = null;
        try {
            font = BaseFont.createFont(fontFile, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        } catch (Exception e) {
            try {
                log.debug("Could not find font '"+fontFile+"', trying default windows font");
                font = BaseFont.createFont("c:\\windows\\fonts\\MSMINCHO.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            } catch (Exception e1) {
                throw new RenderingException("Could not initialize japanese font", e1);
            }
        }
        return font;
    }

    public static BaseFont getLatinFont(String fontFile) throws RenderingException {
        BaseFont font = null;
        try {
            font = BaseFont.createFont(fontFile, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        }
        catch(Exception e) {
            throw new RenderingException("Could not initialize latin font", e);
        }
        return font;
    }
}
